package com.onehash.onebilling.util;


import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
/**
 * Author Xia Xi
 */

public class PaymentFileGenerator {
		
		private static Random ran = new Random();

		private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
		
		public static void main(String[] args) {
			
			String fileName = "data\\payment.csv";
			String delimiter = ",";
			
			/*
			int customerNo = 5;
			String [] customerAcc = new String [customerNo];
			
			for (int n = 0; n < customerNo; n++) {
				if (n <10){
					customerAcc [n] = "CA000"+n;
				}else if (n <100) {
					customerAcc [n] ="CA00"+n;
					
				}else if (n<1000){
					customerAcc[n] ="CA0"+n;
				}
			}
			*/
			
			String [] customerAcc = {"CA0001", "CA0005", "CA0008"}; //hardcode your account number here
			String [] paymentMethod ={"Singpost","AXS","IBanking"};
			
			
			int aveCustomerMonthlyRecord = 2; // how many average monthly record you want to generate
			int totalRecord = aveCustomerMonthlyRecord*14*customerAcc.length; 
			//from last year Jan to this year Feb , total 14 months
			
			
			try {

				 
					  FileOutputStream fostream = new FileOutputStream(fileName);
					  PrintStream p = new PrintStream( fostream );
					  
					 for( int n = 0; n< totalRecord; n++){
						 //Account, yyyyMMdd_HHmmss,doubleAmount,IBanking
						 String strLine = getRandomAccount(customerAcc) +delimiter+getRandomDateTime() +delimiter+ getRandomPayAmount()+delimiter+ getRandomPaymentMethod(paymentMethod);
						  p.println(strLine);
					 }
					
						 
					//fileclose
					p.close();
					fostream.close();
					System.out.println("Generated total "+ totalRecord +" Payment Records to "+fileName);
						 
				
			}catch (IOException ioe){
				 ioe.printStackTrace();
			 }

		}
		
		
		public static String getRandomAccount (String [] customerAcc){
			int roll = ran.nextInt(customerAcc.length);
			return customerAcc[roll];
		}
		public static String getRandomPaymentMethod(String [] paymentMethod){
			int roll = ran.nextInt(paymentMethod.length);
			return paymentMethod[roll];
		}
		
		
		public static String getRandomDateTime(){ //yyyyMMdd_HHmmss from 20110101 to 20120229
			try {
				Date start = sdf.parse("20110101_000000");
				Date end = sdf.parse("20120229_235959");
				long date = random (start.getTime(), end.getTime());
				Date ranDate = new Date(date);
				return sdf.format(ranDate);
			}catch (Exception e) {
				e.printStackTrace();
			}
			return "20120203_123456"; //default
		}
		

		private static long random(long begin,long end){
			long rtn = begin + (long)(Math.random() * (end - begin));

			if(rtn == begin || rtn == end){
				return random(begin,end);
			}
			return rtn;
		}
		public static double getRandomPayAmount(){
			double roll =  ran.nextInt(50) + 0.1*ran.nextInt(10); //average amount paid , set here
			return roll;
		}
}

//TODO method for paymentRecord, use <customAcc, ArrayList <payment>>
//method to get current month payment
//method to calculate total payment until now
//deregistration shouldn't affact this records.




